<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * BaseController
 *
 * All controller requests go here first, so it's a convenient spot to
 * call all the auth, header, breadcrumb stuff.
 *
 * @package		SimplyPost
 * @subpackage	Libraries
 * @author		Pascal Kriete
 */

class BaseController extends Controller {

	/* Store footer for destruct */
	var $footer;
	
	function BaseController()
	{
		parent::Controller();

		//Global Settings
		$query = $this->db->get('general', 1, 0);
		$glbl = $query->row_array();
		
		// Set the default language to our global language
		$this->config->set_item('language', $glbl['language']);
		
		// Config Dependant Constants (others set in constants.php)
		define('SP_VERSION'		,$this->config->item('sp_version'));
		define('BOARD_TITLE'	,$glbl['title']);
		define('BOARD_LOCKED'	,$glbl['locked']);
		define('AJAX'			,isset($_POST['ajax']));
		define('TEMPLATE'		,'default'.'/');

		unset($_POST['ajax']);
		unset($glbl);

		// Check the request for css and js
		$this->_check_request();
				
		// Load the error language file (others loaded where needed)
		$this->lang->load('error');

		// Login logic handled by access lib
		$this->load->library('access');
		
		//Forum locked (checks for superadmin)?
		$locked = $this->access->_check_locked();
		if ($locked && $this->uri->uri_string() != '/member/login')
		{
			$this->auth->logout();
			$this->session->set_flashdata('msg', $this->lang->line('error_board_locked'));
			redirect('member/login');
		}
				
		//Load Plugins
		$this->load->library('plugin');
		$this->plugin->register();
		
		//Load Parser
		$this->load->library('parser');
		
		// Ajax requests don't need any of the other view components.  Makes for weird responses.
		if (!AJAX)
		{
			// Specific headers
			if (!$this->logged_in)
				$this->parser->parse('global/regular_header');
			else
				$this->parser->parse('global/member_header');

			// Prep session
			if ($this->session->userdata('editor') === FALSE)
			{
				$this->session->set_userdata('editor', 1);
				$this->session->set_userdata('popup', 1);
				$this->session->set_userdata('toggle', 0);
			}
			
			// Load breadcrumb library
			$this->load->library('breadcrumb');
			
			// Show flashdata message
			$data['flash_msg'] = $this->session->flashdata('msg');
			$this->parser->parse('global/message', $data);
			
			// $this->load isn't available in the destructor => get the footer string
			$this->footer = $this->parser->parse('global/footer', array(), TRUE);
		}
		else
		{
			$this->footer = '';
		}
		
		// PHP 4 doesn't do __destruct
		register_shutdown_function(array(&$this, "shutdown"));
	}
	
	function _check_request()
	{
		if (defined('CSS'))
		{
			header('Content-Type: text/css');
			include(TMPPATH.TEMPLATE.'assets/css/'.CSS);
			exit;
		}
		
		if (defined('JS'))
		{
			header('Content-Type: text/javascript');
			include(TMPPATH.TEMPLATE.'assets/js/'.JS);
			exit;
		}
		return;
	}
	
	function shutdown()
	{
		echo $this->footer;
	}
}

// --------------------------------------------------------------------

/**
 * Authenticated
 *
 * Extend this to force authentication on the whole controller
 *
 * @package		SimplyPost
 * @subpackage	Libraries
 * @author		Pascal Kriete
 */
class Authenticated extends BaseController {

	function Authenticated()
	{
		parent::BaseController();
		
		$this->access->restrict('user', 'error_requires_auth');
	}
}

// --------------------------------------------------------------------

/**
 * Administrator
 *
 * Extend this to make a controller admin only
 *
 * @package		SimplyPost
 * @subpackage	Libraries
 * @author		Pascal Kriete
 */
class Administrator extends Authenticated {

	function Administrator()
	{
		parent::Authenticated();
		
		$this->access->restrict('admin', 'error_requires_admin');
	}
}